package DAO.impl;

import DAO.BaseDao;
import DAO.OrdersDAO;
import bean.Orders;

import java.sql.Connection;
import java.util.List;

/**
 * @author 栋
 * @date 2022/11/10
 * @description
 */
public class OrderDAOImpl extends BaseDao<Orders> implements OrdersDAO {
    @Override
    public int add_order(Connection conn, Orders o) {
        String sql="insert into orders(order_id,user_id,item_id,method,item_name,buy_count,total_price,buy_date,buy_time,address,arrival) values(?,?,?,?,?,?,?,?,?,?,?)";
        return update(conn,sql,o.getOrder_id(),o.getUser_id(),o.getItem_id(),o.getMethod(),o.getItem_name(),o.getBuy_count(),o.getTotal_price(),o.getBuy_date(),o.getBuy_time(),o.getAddress(),o.getArrival());
    }

    @Override
    public List<Orders> get_all_orders(Connection conn) {
        String sql="select * from orders";
        return getForList(conn,sql);
    }

    @Override
    public List<Orders> getOrdersByUser_id(Connection conn, String user_id) {
        String sql="select * from orders where user_id=?";
        return getForList(conn,sql,user_id);
    }

    @Override
    public List<Orders> getOrderByOwer_id(Connection conn, String ower_id) {
        String sql="SELECT o.order_id,o.user_id,o.item_id,o.method,o.item_name,o.buy_count,o.total_price,o.buy_date,o.buy_time,o.address,o.arrival FROM(SELECT * FROM items WHERE Ower_id=?) i JOIN orders o ON i.item_id=o.item_id";
        return getForList(conn,sql,ower_id);
    }

    @Override
    public Orders getOrderById(Connection conn, String order_id) {
        String sql="select * from orders where order_id=?";
        return getInstance(conn,sql,order_id);
    }

    @Override
    public int updateOrder(Connection conn, Orders order) {
        String sql="update orders set arrival=? where order_id=?";
        return update(conn,sql,order.getArrival(),order.getOrder_id());
    }
}
